<template>
  <el-dialog title="产品BOM物料选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
    <el-table
        ref="bomTable"
        v-loading="loading"
        :data="bomList"
        @selection-change="handleSelectionChange"
        @row-dblclick="handleRowDbClick"
        style="width: 100%"
    >
      <el-table-column
          type="selection"
          width="50"
          align="center"
      />
      <el-table-column label="物料编码" align="center" prop="bomItemCode" />
      <el-table-column label="物料名称" align="center" prop="bomItemName" :show-overflow-tooltip="true" />
      <el-table-column label="规格" align="center" prop="bomItemSpec" :show-overflow-tooltip="true" />
      <el-table-column label="单位" width="60px" align="center" prop="unitOfMeasure" />
      <el-table-column label="使用比例" width="90px" align="center" prop="quantity" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { listBom } from "@/api/mes/md/bom";

export default {
  name: "ItemBomSelectMulti",
  props: {
    itemId: undefined,
    bomVersionId: undefined
  },
  data() {
    return {
      showFlag: false,
      // 遮罩层
      loading: true,
      selectedRows: [],  // 更改为数组存储多选的数据
      // 总条数
      total: 0,
      // 产品BOM关系表格数据
      bomList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemId: this.itemId,
        bomVersionId: this.bomVersionId,
        bomItemId: null,
        bomItemCode: null,
        bomItemName: null,
        bomItemSpec: null,
        unitOfMeasure: null,
        quantity: null,
        enableFlag: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
      }
    };
  },
  watch: {
    bomVersionId: function(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.queryParams.bomVersionId = newVal;
        this.getList();
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品BOM关系列表 */
    getList() {
      this.loading = true;
      listBom(this.queryParams).then(response => {
        console.log('子页面 lsit集合',response.rows);
        this.bomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
   // 刷新数据的方法
    refreshData(itemId,bomVersionId, bomVersion) {
      console.log('bom 子页面刷新 refreshData--bomVersionId',bomVersionId);
      console.log('bom 子页面刷新 refreshData--bomVersion',bomVersion);
      console.log('bom 子页面刷新 refreshData--itemId',itemId);
      this.queryParams.itemId=itemId;
      this.queryParams.bomVersionId = bomVersionId;
      this.queryParams.bomVersion = bomVersion;
      this.getList();
    },
    handleSelectionChange(selection) {
      this.selectedRows = selection;  // 更新选中的行
    },

    handleRowDbClick(row) {
      if (row) {
        const index = this.selectedRows.findIndex(item => item.bomItemId === row.bomItemId);
        if (index > -1) {
          this.selectedRows.splice(index, 1);  // 如果已选中，则取消选择
        } else {
          this.selectedRows.push(row);  // 如果未选中，则添加到选中列表
        }
        this.$refs.bomTable.toggleRowSelection(row, !this.selectedRows.includes(row));  // 切换行的选择状态
      }
    },

    // 确定选中
    confirmSelect() {
      if (this.selectedRows.length === 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected', this.selectedRows);  // 发送选中的数据
      this.showFlag = false;
    }
  }
};
</script>
